//
//  ImgLoopController.m
//  BIN网易新闻
//
//  Created by mac on 16/1/20.
//  Copyright © 2016年 BINBINLULU. All rights reserved.
//

#import "ImgLoopController.h"
#import "ImgLoopCell.h"

@interface ImgLoopController ()
@property(nonatomic,strong)NSArray* ImgLoopmodels;
// 布局对象
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;


@end

@implementation ImgLoopController

- (NSArray *)ImgLoopmodels{
    if (_ImgLoopmodels == nil) {
        _ImgLoopmodels= @[@"ad_00",@"ad_01",@"ad_02",@"ad_03",@"ad_04"];
    }
    return _ImgLoopmodels;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
}

/// cantainer大小确定以后再设置layout
- (void)viewDidLayoutSubviews{
    [self setcollectionlayout];
}

/// 设置collection的布局
- (void)setcollectionlayout{
    self.layout.itemSize=self.collectionView.frame.size;
    
    self.layout.minimumInteritemSpacing=0;
    
    self.layout.minimumLineSpacing=0;
    
    self.layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.bounces=NO;
    
    self.collectionView.pagingEnabled=YES;
    
    self.collectionView.showsHorizontalScrollIndicator=YES;
    
}

// 代理方法
/// 滚动结束的时候
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    // 获取当前滚动的索引
    NSInteger index=self.collectionView.contentOffset.x/self.collectionView.frame.size.width;
    
    // 获取该滚到第几个cell
    NSInteger itemnum=index%self.ImgLoopmodels.count;
    
    NSIndexPath *indexpath=[NSIndexPath indexPathForRow:itemnum inSection:1];
    
    [self.collectionView scrollToItemAtIndexPath:indexpath atScrollPosition:0 animated:NO];
    
}

//数据源方法
/// 返回组数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
/// 返回每一组的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.ImgLoopmodels.count;
}
/// 返回cell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImgLoopCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImgCell" forIndexPath:indexPath];
    
    cell.pagecontrol.currentPage = indexPath.item;
    
    cell.loop_imageview.image  = [UIImage imageNamed:self.ImgLoopmodels[indexPath.item]];
    
    
    return cell;
}




@end
